﻿Namespace DotNetNuke.Modules.ActiveForums.Controls
    Public Class TopicBrowser
        Public Property PortalId() As Integer = -1
        Public Property ModuleId() As Integer = -1
        Public Property TabId() As Integer = -1
        Public Property ForumIds() As String = String.Empty
        Public Property ForumId() As Integer = -1
        Public Property ForumGroupId() As Integer = -1
        Public Property ParentForumId() As Integer = -1
        Public Property TopicId() As Integer = -1
        Public Property Topic() As String = String.Empty
        Public Property Template() As String = String.Empty
        Public Property HeaderTemplate() As String = String.Empty
        Public Property FooterTemplate() As String = String.Empty
        Public Property ForumUser() As User
        Public Property CategoryId() As Integer = -1
        Public Property TagId() As Integer = -1
        Public Property PageIndex() As Integer = 1
        Public Property PageSize() As Integer = 20
        Public Property ItemCss() As String = "aftb-topic"
        Public Property AltItemCSS() As String = "aftb-topic-alt"
        Public Property UseAjax() As Boolean = False
        Public Property ImagePath() As String = String.Empty
        Public Property MaintainPage() As Boolean = False
        Private _mainSettings As SettingsInfo = Nothing
        Private _canEdit As Boolean = False
        Public Property TimeZoneOffset As Integer = 0
        Public Function Render() As String
            Dim fc As New ForumController
            Dim fs As String = fc.GetForumsForUser(ForumUser.UserRoles, PortalId, ModuleId, "CanEdit")
            If Not String.IsNullOrEmpty(fs) Then
                _canEdit = True
            End If
            Dim sb As New Text.StringBuilder()
            Dim forumPrefix As String = String.Empty
            Dim groupPrefix As String = String.Empty
            _mainSettings = DataCache.MainSettings(ModuleId)
            If _mainSettings.URLRewriteEnabled Then
                If ForumId > 0 Then
                    Dim f As Forum = fc.GetForum(PortalId, ModuleId, ForumId)
                    If Not f Is Nothing Then
                        forumPrefix = f.PrefixURL
                        groupPrefix = f.ForumGroup.PrefixURL
                    End If
                ElseIf ForumGroupId > 0 Then
                    Dim grp As New ForumGroupController
                    Dim g As ForumGroupInfo = grp.Groups_Get(ModuleId, ForumGroupId)
                    If Not g Is Nothing Then
                        groupPrefix = g.PrefixURL
                    End If
                End If
            End If

            Dim tmp As String = String.Empty
            Dim db As New Data.Topics
            Dim recordCount As Integer = 0
            Dim i As Integer = 0
            sb.Append(HeaderTemplate)
            Using dr As IDataReader = db.TopicsList(PortalId, PageIndex, PageSize, ForumIds, CategoryId, TagId)
                While dr.Read()
                    If recordCount = 0 Then
                        recordCount = Integer.Parse(dr("RecordCount").ToString)
                    End If
                    tmp = ParseDataRow(dr, Template)
                    If i Mod 2 = 0 Then
                        tmp = tmp.Replace("[ROWCSS]", ItemCss)
                    Else
                        tmp = tmp.Replace("[ROWCSS]", AltItemCSS)
                    End If
                    i += 1
                    sb.Append(tmp)
                End While
                dr.Close()
            End Using
            sb.Append(FooterTemplate)
            Dim pageCount As Integer = 1
            pageCount = CType(System.Math.Ceiling(recordCount / PageSize), Integer)
            Dim cUtils As New ControlUtils
            Dim otherPrefix As String = String.Empty
            If TagId > 0 Or CategoryId > 0 Then
                Dim id As Integer = -1
                If TagId > 0 Then
                    id = TagId
                Else
                    id = CategoryId
                End If
                Using dr As IDataReader = DataProvider.Instance.Tags_Get(PortalId, ModuleId, id)
                    While dr.Read
                        otherPrefix = Utilities.CleanName(dr("TagName").ToString)
                    End While
                    dr.Close()
                End Using
            End If
            sb.Append(cUtils.BuildPager(TabId, ModuleId, groupPrefix, forumPrefix, ForumGroupId, ForumId, TagId, CategoryId, otherPrefix, PageIndex, pageCount))
            Return sb.ToString
        End Function
        Private Function ParseDataRow(ByVal row As IDataRecord, ByVal tmp As String) As String
            Try
                tmp = tmp.Replace("[AVATAR]", "[AF:AVATAR]")
                For i As Integer = 0 To row.FieldCount - 1
                    Dim name As String = row.GetName(i)
                    Dim k As String = "[" & name.ToUpperInvariant & "]"
                    Dim value As String = row(i).ToString
                    Select Case row(i).GetType().ToString
                        Case "System.DateTime"
                            value = Utilities.GetDate(CDate(row(i).ToString), ModuleId, TimeZoneOffset)
                    End Select
                    tmp = tmp.Replace(k, value)
                Next

                Dim cUtils As New ControlUtils
                Dim auth As New Author
                Dim columnPrefix As String = "Topic"
                If CInt(row("ReplyId").ToString) > 0 Then
                    columnPrefix = "Reply"
                    auth.DisplayName = row(columnPrefix & "AuthorDisplayName").ToString
                Else
                    auth.DisplayName = row("TopicAuthorName").ToString
                End If
                auth.AuthorId = Integer.Parse(row(columnPrefix & "AuthorId").ToString)

                auth.LastName = row(columnPrefix & "AuthorLastName").ToString
                auth.FirstName = row(columnPrefix & "AuthorFirstName").ToString
                auth.Username = row(columnPrefix & "AuthorUsername").ToString

                tmp = tmp.Replace("[TOPICURL]", cUtils.TopicURL(row, TabId, ModuleId))
                tmp = tmp.Replace("[FORUMURL]", cUtils.ForumURL(row, TabId, ModuleId))
                If Integer.Parse(row("LastAuthorId").ToString) = -1 Then
                    Try
                        tmp = tmp.Replace("[LASTAUTHOR]", UserProfiles.GetDisplayName(ModuleId, _mainSettings.ProfileVisibility, ForumUser.Profile.IsMod, -1, _mainSettings.UserNameDisplay, auth))
                    Catch ex As Exception
                        tmp = tmp.Replace("[LASTAUTHOR]", "anon")
                    End Try

                Else
                    tmp = tmp.Replace("[LASTAUTHOR]", UserProfiles.GetDisplayName(ModuleId, _mainSettings.ProfileVisibility, ForumUser.Profile.IsMod, Integer.Parse(row("LastAuthorId").ToString), _mainSettings.UserNameDisplay, auth))
                End If

                If _canEdit Then
                    tmp = tmp.Replace("[AF:QUICKEDITLINK]", "<span class=""af-icon16 af-icon16-gear"" onclick=""amaf_quickEdit(" & row("TopicId").ToString & ");""></span>")
                Else
                    tmp = tmp.Replace("[AF:QUICKEDITLINK]", String.Empty)
                End If
                '

                tmp = tmp.Replace("[TOPICSTATE]", cUtils.TopicState(row))
                Dim sAvatar As String = String.Empty
                If _mainSettings.ProfileType = ProfileTypes.Social Then
                    sAvatar = "<span style=""background-image:url('/desktopmodules/activesocial/profilepic.ashx?PortalId=" & PortalId.ToString & "&uid=" & row("LastAuthorId").ToString & "&h=26&w=26');""></span>"
                    '"<social:ProfilePicture ProfileUserId='" & up.UserId & "' PicSizes='sm' imagealt='" & UserProfiles.GetDisplayName(ModuleId, up.UserId, MainSettings.UserNameDisplay, True, up.UserName, up.FirstName, up.LastName, up.DisplayName).Replace("'", String.Empty).Replace("""", String.Empty) & "'  runat='server'/>"
                Else
                    sAvatar = UserProfiles.GetAvatar(auth.AuthorId, PortalId, ImagePath, _mainSettings.Theme, row("Avatar").ToString, Integer.Parse(row("AvatarType").ToString), _mainSettings.AvatarWidth, _mainSettings.ProfileType, True)
                End If
                tmp = tmp.Replace("[AF:AVATAR]", sAvatar)
                Return tmp
            Catch ex As Exception
                Return ex.Message
            End Try

        End Function
       
    End Class
End Namespace

